package com.scau.database.service.storemanager;

import ch.qos.logback.classic.gaffer.PropertyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scau.data.entity.*;
import com.scau.database.common.service.ImportMapperService;
import com.scau.database.entity.storemanager.*;
import com.scau.database.enums.*;
import com.scau.database.utils.WordBuidUtil;
import com.scau.database.utils.entity.OrderInWordTableVO;
import freemarker.template.TemplateException;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.context.IContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class StoreManagerService {
    private final static Integer PAGE_SIZE = 20;
    private final static Integer CREATE_STOCK_VERSION = 2;
    private final static String RETURN_GOODS = "退货";
    private final static String EXCHANGE_GOODS = "换货";
    private final static String ERROR = "数据异常，请与管理员联系";
    // 退货的deal_method
    private static final String RET_DESTRUCTION = "销毁";
    private static final String RET_BACK_FACTORY = "退返厂";
    private static final String RET_ENTERWARE = "进入仓库";
    // 换货的deal_method
    private static final String EXG_ENTERWARE = "进入仓库";
    private static final String EXG_SEND_TO_STORE = "发往店家";
    private static final String EXG_BACK_FACTORY = "换返厂";
    private static final String EXG_DESTRUCTION = "销毁";

    // 换货的状态
    private static final String EXG_NOT_DEAL = ExchangeGoodsFinishedEnum.NOT_DEAL.getDescription();
    private static final String EXG_SEND_APPLY = ExchangeGoodsFinishedEnum.SEND_APPLY.getDescription();
    private static final String EXG_FINISHED = ExchangeGoodsFinishedEnum.FINISHIED_DEAL.getDescription();
    // 退货的状态
    private static final String RET_NOT_DEAL = "未处理";
    private static final String RET_FINISHED = "已处理";
    // 默认三包服务
    private static final Integer DEFAULT_RETURN_DATS = 0;
    private static final Integer DEFAULT_REPAIR_DATS = 0;
    private static final Integer DEFAULT_EXCHANGE_DATS = 0;






    @Autowired
    private ImportMapperService importMapperService;


    public EnterDTO getEnters(Integer id,Long page) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        EnterDTO enterDTO = new EnterDTO(); // 返回的对象
        QueryWrapper<NotEnterOrderDO> notEnterOrderDOQueryWrapper = new QueryWrapper<>();
        notEnterOrderDOQueryWrapper.eq("warehouse_id",id); // 查询条件
        Page<NotEnterOrderDO> notEnterOrderDOPage = new Page<>(page, PAGE_SIZE); // 分页查询
        IPage<NotEnterOrderDO> iPage = importMapperService.notEnterOrderMapper.selectPage(notEnterOrderDOPage, notEnterOrderDOQueryWrapper);
        List<NotEnterOrderDO> records = iPage.getRecords(); // 拿到结果
        Long pageCount = iPage.getPages();

        // 封装数据表
        List<EnterVO> enterVOS = new ArrayList<>();
        Iterator<NotEnterOrderDO> iterator = records.iterator();
        while (iterator.hasNext()){
            NotEnterOrderDO next = iterator.next();
            EnterVO enterVO = new EnterVO();

            PropertyUtils.copyProperties(enterVO,next); // 将属性进行赋值
            enterVO.setBar_code(next.getBarCode());
            enterVO.setOrder_number(next.getOrderNumber());
            enterVO.setUnit_price(next.getUnitPrice());
            enterVO.setName(next.getCommodity());
            enterVO.setFactory(next.getFactoryName());
            if (enterVO.getFinishiedEnterWare() == null){
                enterVO.setFinishiedEnterWare(0);
            }
            if (enterVO.getCount() != null && enterVO.getFinishiedEnterWare() != null){
                enterVO.setMaxEnter(enterVO.getCount() - enterVO.getFinishiedEnterWare());
            }

            enterVOS.add(enterVO); // 加入列表
        }

        // 封装对象
        enterDTO.setCurPage(page);
        enterDTO.setPageCount(pageCount);
        enterDTO.setEnters(enterVOS);
        return enterDTO;
    }

    /**
     * 入库操作
     * 1、检查数据是否异常
     * 2、插入生成的enter_ware表
     * 3、查看是否存在对应库存表
     * 4、更新库存表
     * @param dto
     * @return
     */
    @Transactional
    public Integer doEnter(DoEnterDTO dto){
        Long order_number = dto.getEnter().getOrder_number();
        Integer index = dto.getEnter().getIndex();

        QueryWrapper<OrderLineDO> orderLineDOQueryWrapper = new QueryWrapper<>();
        orderLineDOQueryWrapper.eq("order_number",order_number)
                .eq("`index`",index);
        OrderLineDO orderLineDO = importMapperService.orderLineMapper.selectOne(orderLineDOQueryWrapper);
        if (orderLineDO == null)return 2; // 不存在该订购单

        Integer count = orderLineDO.getCount(); // 订购数量
        QueryWrapper<EnterWareDO> enterWareDOQueryWrapper = new QueryWrapper<>();
        enterWareDOQueryWrapper.eq("order_number",order_number)
                .eq("`index`",index);
        List<EnterWareDO> enterWareDOS = importMapperService.enterWareMapper.selectList(enterWareDOQueryWrapper);
        int sum = enterWareDOS.stream().mapToInt(EnterWareDO::getCount).sum();
        if (sum + dto.getEnter().getCount() > count)return 3; // 入库数量大于进货数量

        EnterWareDO enterWareDO = new EnterWareDO();
        enterWareDO.setBarCode(dto.getEnter().getBar_code());
        enterWareDO.setCount(dto.getEnter().getCount());
        enterWareDO.setInChargePerson(dto.getUserName());
        enterWareDO.setIndex(dto.getEnter().getIndex());
        enterWareDO.setTime(new Date());
        enterWareDO.setWarehouseId(dto.getWarehouseId());
        enterWareDO.setOrderNumber(dto.getEnter().getOrder_number());
        importMapperService.enterWareMapper.insert(enterWareDO);

        // 更新库存表
        QueryWrapper<StockDO> stockDOQueryWrapper = new QueryWrapper<>();
        stockDOQueryWrapper.eq("bar_code",dto.getEnter().getBar_code())
                .eq("warehouse_id",dto.getWarehouseId());
        Integer integer = importMapperService.stockMapper.selectCount(stockDOQueryWrapper);
        if (integer < 1){
            // 若还未有记录
            StockDO stockDO = new StockDO();
            stockDO.setBarCode(dto.getEnter().getBar_code());
            stockDO.setWarehouseId(dto.getWarehouseId());
            stockDO.setStock(dto.getEnter().getCount());
            int insert = importMapperService.stockMapper.insert(stockDO);
        }else {
            Integer result = importMapperService.stockMapper.addStock(dto.getEnter().getCount(),
                    dto.getEnter().getBar_code(), dto.getWarehouseId());
            //  通过返回值判断是否成功，否则抛出异常，回滚
            if (result != 1){
                    throw new MybatisPlusException("库存更新失败");
            }
        }

        return 1;
    }

    /**
     * 查询上架申请
     * @param warehouseId
     * @return
     */
    public List<PassVO> getApplies(Integer warehouseId){
        List<PassDTO> applies = importMapperService.shelfApplyMapper.getApplies(warehouseId);
        List<PassVO> passVOS = new ArrayList<>();
        Iterator<PassDTO> iterator = applies.iterator();
        while (iterator.hasNext()){
            PassDTO next = iterator.next();
            PassVO passVO = new PassVO();
            passVO.setBar_code(next.getBarCode());
            passVO.setBrand(next.getBrand());
            passVO.setCommodity(next.getCommodity());
            passVO.setCount(next.getCount());
            passVO.setStock(next.getStock());
            passVO.setId(next.getId());

            StoreDO storeDO = new StoreDO();
            storeDO.setId(next.getStoreId());
            storeDO.setAddress(next.getAddress());
            storeDO.setLeader(next.getLeader());
            storeDO.setName(next.getStoreName());
            passVO.setStore(storeDO);
            passVOS.add(passVO);
        }
        return passVOS;
    }

    /**
     * 通过上架申请
     * 0、检查库存
     * 1、查看上架申请是否存在
     * 2、将shelf_apply的对应行复制到shelf_ok
     * 3、将shelf_apply的行删除
     * 4、更新库存
     * @param passVO
     * @param warehouseId
     * @return
     */
    @Transactional
    public Integer passApply(PassVO passVO,Integer warehouseId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        // 检查库存
        QueryWrapper<StockDO> stockDOQueryWrapper = new QueryWrapper<>();
        stockDOQueryWrapper.eq("bar_code",passVO.getBar_code())
                .eq("warehouse_id",warehouseId);
        StockDO stockDO = importMapperService.stockMapper.selectOne(stockDOQueryWrapper);
        if (stockDO == null)return 3; //库存不足
        Integer stock = stockDO.getStock();
        if (passVO.getCount() > stock)return 3; // 库存不足

        // 确认存在
        QueryWrapper<ShelfApplyDO> shelfApplyDOQueryWrapper = new QueryWrapper<>();
        shelfApplyDOQueryWrapper.eq("id",passVO.getId())
                       .eq("warehouse_id",warehouseId);
        Integer hasApply = importMapperService.shelfApplyMapper.selectCount(shelfApplyDOQueryWrapper);
        if (hasApply < 1)return 2; // 数据非法

        // 复制到ok表
        ShelfApplyDO shelfApplyDO = importMapperService.shelfApplyMapper.selectOne(shelfApplyDOQueryWrapper);
        ShelfOKDO shelfOKDO = new ShelfOKDO();
        PropertyUtils.copyProperties(shelfOKDO,shelfApplyDO);
        shelfOKDO.setSurplus(shelfApplyDO.getCount()); // 余量初始就是申请的数目
        System.out.println(shelfOKDO);
        importMapperService.shelfOKMapper.insert(shelfOKDO); // 保存

        // 删除apply表数据
        int deleteOk = importMapperService.shelfApplyMapper.delete(shelfApplyDOQueryWrapper);

        // 更新库存
        Integer isUpdeate = importMapperService.stockMapper.reduceStock(passVO.getCount(), passVO.getBar_code(), warehouseId);
        if (isUpdeate != 1){
            throw new MybatisPlusException("更新库存失败");
        }
        // 更新on_sell表
        QueryWrapper<OnSellDO> onSellDOQueryWrapper = new QueryWrapper<>();
        onSellDOQueryWrapper.eq("store_id",shelfOKDO.getStoreId())
                .eq("bar_code",shelfOKDO.getBarCode());
        OnSellDO onSellDO = importMapperService.onsellMapper.selectOne(onSellDOQueryWrapper);
        if (onSellDO != null){
            // 增加on_sell的surplus,count
            importMapperService.onsellMapper.addSurplus(shelfOKDO.getStoreId(),
                    shelfOKDO.getBarCode(),
                    shelfOKDO.getSurplus());
            importMapperService.onsellMapper.addCount(shelfOKDO.getStoreId(),
                    shelfOKDO.getBarCode(),
                    shelfOKDO.getCount());

        }else {
            // 创建on_sell表
            onSellDO = new OnSellDO();
            // 其他数值默认以 commodity_info 为准
            QueryWrapper<CommodityInfoDO> commodityInfoDOQueryWrapper = new QueryWrapper<>();
            commodityInfoDOQueryWrapper.eq("bar_code",shelfOKDO.getBarCode());
            CommodityInfoDO commodityInfoDO = importMapperService.commodityInfoMapper.selectOne(commodityInfoDOQueryWrapper);

//            PropertyUtils.copyProperties(onSellDO,commodityInfoDO); // 使用工具赋值
            onSellDO.setStoreId(shelfOKDO.getStoreId());
            onSellDO.setBarCode(shelfOKDO.getBarCode());
            onSellDO.setOriginalPrice(commodityInfoDO.getRecommendPrice());
            onSellDO.setSpecialPrice(commodityInfoDO.getRecommendPrice());
            onSellDO.setUseSpecialPrice(true);
            onSellDO.setCount(shelfOKDO.getCount());
            onSellDO.setSurplus(shelfOKDO.getSurplus());
            // 默认店保为0
            onSellDO.setRepairDays(DEFAULT_REPAIR_DATS);
            onSellDO.setReturnDays(DEFAULT_RETURN_DATS);
            onSellDO.setExchangeDays(DEFAULT_EXCHANGE_DATS);

            // 保存
            int insert = importMapperService.onsellMapper.insert(onSellDO);

        }
        return 1;
    }

    /**
     * 搜索售后记录：
     * 1、退货
     * 2、换货
     * @param warehouseId
     * @return
     */
    public List<ServiceVO> getService(Integer warehouseId){
        List<ReturnDTO> needDealReturnInfos = importMapperService.returnGoodsMapper.getNeedDealReturnInfos(warehouseId);
//        System.out.println(needDealReturnInfos);
        List<ServiceVO> serviceVOS  = new ArrayList<>();
        Iterator<ReturnDTO> iterator = needDealReturnInfos.iterator();
        while(iterator.hasNext()){
            ReturnDTO next = iterator.next();
            ServiceVO serviceVO = new ServiceVO();
            // 封装VO
            serviceVO.setServiceType(RETURN_GOODS); // 退货
            serviceVO.setBar_code(next.getBarCode());
            serviceVO.setCount(next.getCount());
            serviceVO.setDeal_id(next.getDealId());
            serviceVO.setDeal_status(IsDealEnum.NOT_DEAL.getDescription()); // 未处理
            String type = next.getType();
            Integer typeI = null;
            try {
                typeI = Integer.valueOf(type);
            }catch (Exception e){}

            Integer canResell = next.getCanResell();
            if (IsResellEnum.NOT_RESELL.getType().equals(canResell)){
                // 不可再次出售
                serviceVO.setDeal_method(RET_DESTRUCTION);
            }else if (IsResellEnum.CAN_RESELL.getType().equals(canResell)){
                if (ReturnGoodsTypeEnum.FACTORY.getType().equals(typeI) ||
                ReturnGoodsTypeEnum.FACTORY.getDescription().equals(type)){
                    // 厂保
                    // 返厂:[factory_id]factory
                    String factory = next.getFactoryName();
                    Integer factoryId = next.getFactoryId();
                    String dealMethod = RET_BACK_FACTORY+":["+factoryId+"]"+factory;
                    serviceVO.setDeal_method(dealMethod);
                }else if (ReturnGoodsTypeEnum.STORE.getType().equals(typeI) ||
                ReturnGoodsTypeEnum.STORE.getDescription().equals(type)){
                    // 不需仓库要做处理,由售后来维护
                    continue;
//                    String dealMethod = RET_ENTERWARE; // 进入仓库
//                    serviceVO.setDeal_method(dealMethod);
                }else {
                    serviceVO.setDeal_method(ERROR);
                }
            }
            // 封装客户服务中心（售后点）对象
            CustormerServiceDO custormerServiceDO = new CustormerServiceDO();
            custormerServiceDO.setId(next.getServiceId());
            custormerServiceDO.setName(next.getServiceName());
            serviceVO.setCustormer_service(custormerServiceDO);
            // 添加到容器
            serviceVOS.add(serviceVO);
            System.out.println(serviceVO);
        }
        // 下面查询并封装换货的信息
        List<ExchangeDTO> exchangeService = importMapperService.exchangeGoodsMapper.getExchangeService(warehouseId);
        System.out.println(exchangeService);
        Iterator<ExchangeDTO> iterator1 = exchangeService.iterator();
        while (iterator1.hasNext()){
            ExchangeDTO next = iterator1.next();
            // 下面开始封装换货信息
            ServiceVO serviceVO = new ServiceVO();
            // 封装VO
            serviceVO.setServiceType(EXCHANGE_GOODS); // 换货
            serviceVO.setBar_code(next.getBarCode());
            serviceVO.setCount(next.getCount());
            serviceVO.setDeal_id(next.getDealId());
            // 封装售后点对象
            CustormerServiceDO custormerServiceDO = new CustormerServiceDO();
            custormerServiceDO.setName(next.getServiceName());
            custormerServiceDO.setId(next.getServiceId());
            serviceVO.setCustormer_service(custormerServiceDO);
            // 分析状态与处理方法
            if (ExchangeGoodsTypeEnum.EXCHANGE_STORE.getType().equals(next.getType())){
                // 如果是库存换货（店家换货)
                if (IsResellEnum.CAN_RESELL.getType().equals(next.getCanResell())){
                    // 可再次出售 , 不处理
                    continue;
//                    serviceVO.setDeal_method(EXG_ENTERWARE);
//                    serviceVO.setDeal_status(EXG_NOT_DEAL);
                }else if (IsResellEnum.NOT_RESELL.getType().equals(next.getCanResell())){
                    // 不可再次出售
                    serviceVO.setDeal_status(EXG_DESTRUCTION); // 销毁
                    serviceVO.setDeal_status(EXG_NOT_DEAL);
                }else {
                    serviceVO.setDeal_status(ERROR);
                }

            }else if (ExchangeGoodsTypeEnum.EXCHANGE_FACTORY.getType().equals(next.getType())){
                // 厂家换货
                if (ExchangeGoodsFinishedEnum.NOT_DEAL.getType().equals(next.getFinished())){
                    // 未处理
                    Integer factoryId = next.getFactoryId();
                    String factoryName = next.getFactoryName();
                    serviceVO.setDeal_status(EXG_NOT_DEAL);
                    serviceVO.setDeal_method(EXG_BACK_FACTORY+":["+factoryId+"]"+factoryName);
                }else if (ExchangeGoodsFinishedEnum.SEND_APPLY.getType().equals(next.getFinished())){
                    // 发送申请
                    serviceVO.setDeal_status(EXG_SEND_APPLY);
//                    serviceVO.setDeal_method(EXG_ENTERWARE);
                    // 查询购买所在的店铺
                    QueryWrapper<ShopDO> shopDOQueryWrapper = new QueryWrapper<>();
                    shopDOQueryWrapper.eq("id",next.getShopId());
                    ShopDO shopDO = importMapperService.shopMapper.selectOne(shopDOQueryWrapper);
                    QueryWrapper<StoreDO> storeDOQueryWrapper = new QueryWrapper<>();
                    storeDOQueryWrapper.eq("id",shopDO.getStoreId());
                    StoreDO storeDO = importMapperService.storeMapper.selectOne(storeDOQueryWrapper);
                    // 得出处理提示
                    serviceVO.setDeal_method(EXG_SEND_TO_STORE+":["+storeDO.getId()+"]"+storeDO.getName());
                }else {
                    serviceVO.setDeal_status(ERROR);
                }
            }else {
                serviceVO.setDeal_status(ERROR);
            }
            serviceVOS.add(serviceVO);

        }

        return serviceVOS;
    }

    /**
     *  处理售后
     * @param warehouseId
     * @param serviceVO
     * @return
     */
    public Integer doService(Integer warehouseId, ServiceVO serviceVO){
        String serviceType = serviceVO.getServiceType();
        Long deal_id = serviceVO.getDeal_id();
        if (deal_id == null)return 2;
        if (RETURN_GOODS.equals(serviceType)){
            // 退货
            // 1、销毁
            // 2、进入仓库（不会出现这种情况）
            // 3、退返厂
            if (RET_DESTRUCTION.equals(serviceVO.getDeal_method())){
                // 销毁
                // 修改finished字段
                QueryWrapper<ReturnGoodsDO> returnGoodsDOQueryWrapper = new QueryWrapper<>();
                returnGoodsDOQueryWrapper.eq("id",deal_id);
                ReturnGoodsDO returnGoodsDO = importMapperService.returnGoodsMapper.selectOne(returnGoodsDOQueryWrapper);
                returnGoodsDO.setFinished(IsFinishedEnum.IS_FINISHED.getType()); // 已完成
                importMapperService.returnGoodsMapper.update(returnGoodsDO,returnGoodsDOQueryWrapper);
            }else if (RET_ENTERWARE.equals(serviceVO.getDeal_method())){
                // 进入仓库
                // 暂时不会出现（功能由售后完成）
            }else if (serviceVO.getDeal_method() != null && serviceVO.getDeal_method().contains(RET_BACK_FACTORY)){
                // 退返厂
                // 修改finished字段
                QueryWrapper<ReturnGoodsDO> returnGoodsDOQueryWrapper = new QueryWrapper<>();
                returnGoodsDOQueryWrapper.eq("id",deal_id);
                ReturnGoodsDO returnGoodsDO = importMapperService.returnGoodsMapper.selectOne(returnGoodsDOQueryWrapper);
                returnGoodsDO.setFinished(IsFinishedEnum.IS_FINISHED.getType()); // 已完成
                importMapperService.returnGoodsMapper.update(returnGoodsDO,returnGoodsDOQueryWrapper);
            }else {
                return 2; // 数据错误
            }
        }else if (EXCHANGE_GOODS.equals(serviceType)){
            // 换货
            // 1、销毁
            // 2、发往店家
            // 3、换返厂
            if (EXG_DESTRUCTION.equals(serviceVO.getDeal_method())){
                // 销毁
                // 修改finished字段
                QueryWrapper<ExchangeGoodsDO> exchangeGoodsDOQueryWrapper = new QueryWrapper<>();
                exchangeGoodsDOQueryWrapper.eq("id",deal_id);
                ExchangeGoodsDO exchangeGoodsDO = importMapperService.exchangeGoodsMapper.selectOne(exchangeGoodsDOQueryWrapper);
                exchangeGoodsDO.setFinished(ExchangeGoodsFinishedEnum.FINISHIED_DEAL.getType());
                importMapperService.exchangeGoodsMapper.update(exchangeGoodsDO,exchangeGoodsDOQueryWrapper);

            }else if (serviceVO.getDeal_method() != null && serviceVO.getDeal_method().contains(EXG_SEND_TO_STORE)){
                // 发往店家
                // 修改finished字段
                QueryWrapper<ExchangeGoodsDO> exchangeGoodsDOQueryWrapper = new QueryWrapper<>();
                exchangeGoodsDOQueryWrapper.eq("id",deal_id);
                ExchangeGoodsDO exchangeGoodsDO = importMapperService.exchangeGoodsMapper.selectOne(exchangeGoodsDOQueryWrapper);
                exchangeGoodsDO.setFinished(ExchangeGoodsFinishedEnum.FINISHIED_DEAL.getType());
                importMapperService.exchangeGoodsMapper.update(exchangeGoodsDO,exchangeGoodsDOQueryWrapper);
            }else if (serviceVO.getDeal_method() != null && serviceVO.getDeal_method().contains(EXG_BACK_FACTORY)){
                // 换返厂
                // 生成factory_exchange表
                // 修改finished字段

                FactoryExchangeDO factoryExchangeDO = new FactoryExchangeDO();
                String barCode = serviceVO.getBar_code();
                factoryExchangeDO.setBarCode(barCode);
                FactoryDO factoryByBarCode = importMapperService.orderLineMapper.getFactoryByBarCode(barCode);
                factoryExchangeDO.setFactoryId(factoryByBarCode.getId());
                factoryExchangeDO.setCount(serviceVO.getCount());
                factoryExchangeDO.setFinished(false);
                factoryExchangeDO.setServiceId(serviceVO.getCustormer_service().getId());
                factoryExchangeDO.setTime(new Date());
                importMapperService.factoryExchangeMapper.insert(factoryExchangeDO);

                QueryWrapper<ExchangeGoodsDO> exchangeGoodsDOQueryWrapper = new QueryWrapper<>();
                exchangeGoodsDOQueryWrapper.eq("id",deal_id);
                ExchangeGoodsDO exchangeGoodsDO = importMapperService.exchangeGoodsMapper.selectOne(exchangeGoodsDOQueryWrapper);
                exchangeGoodsDO.setFinished(ExchangeGoodsFinishedEnum.SEND_APPLY.getType());
                exchangeGoodsDO.setFactoryExchangeId(factoryExchangeDO.getId());
                importMapperService.exchangeGoodsMapper.update(exchangeGoodsDO,exchangeGoodsDOQueryWrapper);
            }
        }else{
            return 2; // 数据错误
        }
        return 1;
    }


}
